<?php
class Z1kenhnvsController extends AppController {

	var $name = 'Z1kenhnvs';
    var $uses = array('Z1kenhnv','Nhanvien','Loaikenh','Kenh','Phankenh','Donvi','Zdv','Z1xemkenh','Z2xemkenh','Z3xemkenh');
	var $components = array('RequestHandler');
	
	function index($donvi_id=null,$loaikenh_id=null,$nhanvien_id=0)
	{
		$isAjax = 0;		
		if($this->RequestHandler->isAjax())
		{
			$isAjax = 1;
			$this->layout='ajax';
			$loaikenh_id =$this->data['Z1kenhnvs']['loaikenh'];
	        $nhanvien_id =$this->data['Z1kenhnvs']['nhanvien'];
            //print_r($nhanvien_id);
			if ($nhanvien_id=='')
			{
				$this->Z1kenhnv->recursive = 0;
				$this->set('z1kenhnvs', $this->paginate(array('Z1kenhnv.donvi_id'=>$donvi_id,'loaikenh_id'=>$loaikenh_id,'ngaykt_hd'=>null)));
		        $loaikenhs = $this->Z1kenhnv->Loaikenh->find('list',array('fields'=>array('loaikenh')));
		        $nhanvien_id = 0;
	        }
	        else 
	        {
	            $this->Z1kenhnv->recursive = 0;
			    $this->set('z1kenhnvs', $this->paginate(array('loaikenh_id'=>$loaikenh_id,'nhanvien_id'=>$nhanvien_id,'ngaykt_hd'=>null)));
	            $loaikenhs = $this->Z1kenhnv->Loaikenh->find('list',array('fields'=>array('loaikenh')));
	        }
	        $this->set(compact('nhanvien_id','loaikenh_id','loaikenhs','donvi_id','isAjax'));
		}
		else
		{
			$this->Z1kenhnv->recursive = 0;
	        
	        $nhanviens = $this->Z1kenhnv->Nhanvien->find('list',array(
																'conditions'=>array('donvi_id'=>$donvi_id)));
	        $loaikenhs = $this->Z1kenhnv->Loaikenh->find('list',array('fields'=>array('loaikenh')));
	        if(!$loaikenh_id)
	        	$loaikenh_id = key($loaikenhs);
	        
			$this->set('z1kenhnvs', $this->paginate(array('Z1kenhnv.donvi_id'=>$donvi_id,'ngaykt_hd'=>NULL,'loaikenh_id'=>$loaikenh_id)));
	        $this->set(compact('nhanviens','loaikenhs','nhanvien_id','loaikenh_id','donvi_id','d','isAjax'));			
		}
        $d=$this->Donvi->find('first',array('conditions'=>array('id'=>$donvi_id),
	                'fields'=>array('tendv'),'recursive'=>-1));
	        $this->set(compact('d'));

	}
    
    function indexcn($donvicha=null,$loaikenh_id=null,$donvi_id=null,$nhanvien_id=null)
	{
		        
		$isAjax = 0;		
		if($this->RequestHandler->isAjax())
		{
			$isAjax = 1;
			$this->layout='ajax';
			if(!$loaikenh_id)
	   			$loaikenh_id =$this->data['Z1kenhnvs']['loaikenh'];
			
	   		if(!$donvi_id)
				$donvi_id =$this->data['Z1kenhnvs']['donvi'];
	   			
				if(isset($this->data['Z1kenhnvs']['nhanvien']))
					$nhanvien_id =$this->data['Z1kenhnvs']['nhanvien'];
				else 
					$nhanvien_id =$this->data['nhanvien'];
		
			$donvis=$this->Thuocdv->find('list',array('recursive'=>0,'conditions'=>array('donvi_id'=>$donvicha,'left(Donvi.madv,2)'=>'TT'),'fields'=>array('donvi','Donvi.tendv')));
			/*$donvis = $this->Zdv->find('list',array(
                                            'conditions'=>array('idcha'=>$donvicha),'fields'=>array('Zdv.donvi_id','Zdv.tendv')));*/
			
            if(!$donvi_id)
			{
				
                $nhanviens = $this->Z1kenhnv->Nhanvien->find('list',array(
																'conditions'=>array('donvi_id'=>array_keys($donvis))));
                
               	$loaikenhs = $this->Z1kenhnv->Loaikenh->find('list',array('fields'=>array('loaikenh')));
				if(!$nhanvien_id)
				{
					$this->Z1kenhnv->recursive = 0;
	               	$this->set('z1kenhnvs', $this->paginate(array('Z1kenhnv.thuocdv_id'=>$donvicha,'ngaykt_hd'=>NULL,'loaikenh_id'=>$loaikenh_id)));					
				}
				else
				{
					$this->Z1kenhnv->recursive = 0;
					$this->set('z1kenhnvs', $this->paginate(array('Z1kenhnv.nhanvien_id'=>$nhanvien_id,'ngaykt_hd'=>NULL,'loaikenh_id'=>$loaikenh_id)));
				}
               	
            }
			else
            	if(!$nhanvien_id)
            	{
                	$this->Z1kenhnv->recursive = 0;
                	$nhanviens = $this->Z1kenhnv->Nhanvien->find('list',array(
																'conditions'=>array('donvi_id'=>$donvi_id),'fields'=>array('tennv')));
                
                	$loaikenhs = $this->Z1kenhnv->Loaikenh->find('list',array('fields'=>array('loaikenh')));
                	/*$donvis = $this->Zdv->find('list',array(
                                            'conditions'=>array('idcha'=>$donvicha),'fields'=>array('Zdv.donvi_id','Zdv.tendv')));*/
                	$this->set('z1kenhnvs', $this->paginate(array('Z1kenhnv.donvi_id'=>$donvi_id,'ngaykt_hd'=>NULL,'loaikenh_id'=>$loaikenh_id)));
            	}else{
                
                	$nhanviens = $this->Z1kenhnv->Nhanvien->find('list',array(
																'conditions'=>array('donvi_id'=>$donvi_id)));
                	$loaikenhs = $this->Z1kenhnv->Loaikenh->find('list',array('fields'=>array('loaikenh')));
                	/*$donvis = $this->Zdv->find('list',array(
                                            'conditions'=>array('idcha'=>$donvicha),'fields'=>array('Zdv.donvi_id','Zdv.tendv')));*/
					$this->Z1kenhnv->recursive = 0;
					$this->set('z1kenhnvs', $this->paginate(array('Z1kenhnv.nhanvien_id'=>$nhanvien_id,'ngaykt_hd'=>NULL,'loaikenh_id'=>$loaikenh_id)));
            	}
            	
            if(!$donvi_id)
				$donvi_id=0;
			if(!$nhanvien_id)
				$nhanvien_id=0;
	        $this->set(compact('nhanvien_id','loaikenh_id','loaikenhs','donvi_id','isAjax','donvicha','donvis','nhanviens'));
		}
		else
		{
			
			$donvis=$this->Thuocdv->find('list',array('recursive'=>0,'conditions'=>array('donvi_id'=>$donvicha,'left(Donvi.madv,2)'=>'TT'),'fields'=>array('donvi','Donvi.tendv')));
			
			/*$donvis = $this->Zdv->find('list',array(
                'conditions'=>array('idcha'=>$donvicha),'fields'=>array('donvi_id','tendv')));*/
			
			if(!$donvi_id)
				$donvi_id = array_keys($donvis);
	        $nhanviens = $this->Z1kenhnv->Nhanvien->find('list',array(
																'conditions'=>array('donvi_id'=>$donvi_id)));
	        
	        $loaikenhs = $this->Z1kenhnv->Loaikenh->find('list',array('fields'=>array('loaikenh')));
            /*$donvis = $this->Zdv->find('list',array(
                'conditions'=>array('idcha'=>$donvicha),'fields'=>array('donvi_id','tendv')));*/
			
            if(!$loaikenh_id)
            	$loaikenh_id = key($loaikenhs);
            
            
			$this->Z1kenhnv->recursive = 0;
			
			if($nhanvien_id==null) 
			{
				$this->set('z1kenhnvs', $this->paginate(array('Z1kenhnv.thuocdv_id'=>$donvicha,'ngaykt_hd'=>NULL,'loaikenh_id'=>$loaikenh_id)));
			}
			else
			{
				
				if($nhanvien_id==0)
            	{
            		if($donvi_id==0)
            			$donvi_id=$this->Thuocdv->find('list',array('conditions'=>array('donvi_id'=>$donvicha),'fields'=>array('donvi')));
            		
            		$this->Nhanvien->displayField='id';
					$nhanvien_id = $this->Nhanvien->find('list',array('conditions'=>array('donvi_id'=>$donvi_id)));
            	}
            	$this->set('z1kenhnvs', $this->paginate(array('Z1kenhnv.nhanvien_id'=>$nhanvien_id,'ngaykt_hd'=>NULL,'loaikenh_id'=>$loaikenh_id)));
			}
			
			if(is_array($donvi_id))
				$donvi_id=0;
			if(is_array($nhanvien_id))
				$nhanvien_id = 0;
	        $this->set(compact('nhanviens','loaikenhs','nhanvien_id','loaikenh_id','donvi_id','d','isAjax','donvis','donvicha'));			
		}
        $d=$this->Donvi->find('first',array('conditions'=>array('id'=>$donvicha),
	                'fields'=>array('tendv'),'recursive'=>-1));
	        $this->set(compact('d','donvicha'));
	}
	
    function dsnv($donvicha)
    {
        $donvi_id =$this->data['Z1kenhnvs']['donvi'];
        
        if($donvi_id==0)
            	$donvi_id=$this->Thuocdv->find('list',array('conditions'=>array('donvi_id'=>$donvicha),'fields'=>array('donvi')));
        $nhanviens = $this->Z1kenhnv->Nhanvien->find('list',array(
																'conditions'=>array('donvi_id'=>$donvi_id)));
        $this->set(compact('nhanviens','donvi_id','nhanvien_id'));
    }
    
    function indexexcel($donvi_id=null,$loaikenh_id=null,$nhanvien_id=0) {
		$this->Z1kenhnv->recursive = 0;
        $d=$this->Donvi->find('first',array('conditions'=>array('id'=>$donvi_id),
                'fields'=>array('tendv'),'recursive'=>-1));
       
        $nhanviens = $this->Z1kenhnv->Nhanvien->find('list',array(
															'conditions'=>array('donvi_id'=>$donvi_id)));
        $loaikenhs = $this->Z1kenhnv->Loaikenh->find('list',array('fields'=>array('loaikenh')));
		$this->set('z1kenhnvs', $this->Z1kenhnv->find('all',array(
               'conditions'=>array('Z1kenhnv.donvi_id'=>$donvi_id,'ngaykt_hd'=>NULL,'loaikenh_id'=>$loaikenh_id))));
        $this->set(compact('nhanviens','loaikenhs','nhanvien_id','loaikenh_id','donvi_id','d'));
        $this->layout='';			
	}

	function xemkenhnv($donvi_id=null) {
        $this->layout='ajax';
		$loaikenh_id =$this->data['Z1kenhnvs']['loaikenh'];
        $nhanvien_id =$this->data['Z1kenhnvs']['nhanvien'];
		if ($nhanvien_id=='')
		{
		$this->Z1kenhnv->recursive = 0;
		$this->set('z1kenhnvs', $this->paginate(array('Z1kenhnv.donvi_id'=>$donvi_id,'loaikenh_id'=>$loaikenh_id,'ngaykt_hd'=>null)));
        $loaikenhs = $this->Z1kenhnv->Loaikenh->find('list',array('fields'=>array('loaikenh')));
        //debug($this->data);
        }else {
            $this->Z1kenhnv->recursive = 0;
		      $this->set('z1kenhnvs', $this->paginate(array('loaikenh_id'=>$loaikenh_id,'nhanvien_id'=>$nhanvien_id,'ngaykt_hd'=>null)));
                $loaikenhs = $this->Z1kenhnv->Loaikenh->find('list',array('fields'=>array('loaikenh')));
        }
        $this->set(compact('nhanvien_id','loaikenh_id','loaikenhs','donvi_id'));
	}

    function xemkenhnvexcel($donvi_id=null,$loaikenh_id=null,$nhanvien_id=0) {
        $this->layout='';

        $tendv=$this->Zdv->find('first',array(
                'conditions'=>array('donvi_id'=>$donvi_id),
                'fields'=>array('tendv','tendvcha'),'recursive'=>-1));
        $tenloai=$this->Loaikenh->find('first',array(
                'conditions'=>array('id'=>$loaikenh_id),
                'fields'=>array('loaikenh'),'recursive'=>-1));
        //debug($nhanvien_id);
		if ($nhanvien_id==0)
		{
		$this->Z1kenhnv->recursive = 0;
		$this->set('z1kenhnvs', $this->Z1kenhnv->find('all',
                     array('conditions'=>array('Z1kenhnv.donvi_id'=>$donvi_id,'loaikenh_id'=>$loaikenh_id,'ngaykt_hd'=>null))));
        $loaikenhs = $this->Z1kenhnv->Loaikenh->find('list',array('fields'=>array('loaikenh')));
        } else {
            $n=$this->Nhanvien->find('first',array('conditions'=>array('id'=>$nhanvien_id),
                'fields'=>array('donvi_id','manv','tennv'),'recursive'=>-1));
            $this->Z1kenhnv->recursive = 0;
		      $this->set('z1kenhnvs',  $this->Z1kenhnv->find('all',array('conditions'=>array('loaikenh_id'=>$loaikenh_id,'nhanvien_id'=>$nhanvien_id,'ngaykt_hd'=>null))));
            $loaikenhs = $this->Z1kenhnv->Loaikenh->find('list',array('fields'=>array('loaikenh')));
        }
        $this->set(compact('nhanvien_id','loaikenh_id','loaikenhs','donvi_id','n','tendv','tenloai'));
	}
    
    function indexcnexcel($donvicha=null,$loaikenh_id=null,$donvi_id=0,$nhanvien_id=0) {
        $this->layout='';

        $tendv=$this->Zdv->find('first',array(
                'conditions'=>array('donvi_id'=>$donvicha),
                'fields'=>array('tendv','tendvcha'),'recursive'=>-1));
        $tenloai=$this->Loaikenh->find('first',array(
                'conditions'=>array('id'=>$loaikenh_id),
                'fields'=>array('loaikenh'),'recursive'=>-1));
        //debug($nhanvien_id);
        if ($donvi_id==0)
		{
		$this->Z1kenhnv->recursive = 0;
		$this->set('z1kenhnvs', $this->Z1kenhnv->find('all',array('Z1kenhnv.thuocdv_id'=>$donvicha,'ngaykt_hd'=>NULL,'loaikenh_id'=>$loaikenh_id)));
        $loaikenhs = $this->Z1kenhnv->Loaikenh->find('list',array('fields'=>array('loaikenh')));
        }
		else if ($nhanvien_id==0)
		{
		$this->Z1kenhnv->recursive = 0;
		$this->set('z1kenhnvs', $this->Z1kenhnv->find('all',
                     array('conditions'=>array('Z1kenhnv.donvi_id'=>$donvi_id,'loaikenh_id'=>$loaikenh_id,'ngaykt_hd'=>null))));
        $loaikenhs = $this->Z1kenhnv->Loaikenh->find('list',array('fields'=>array('loaikenh')));
        } else {
            $n=$this->Nhanvien->find('first',array('conditions'=>array('id'=>$nhanvien_id),
                'fields'=>array('donvi_id','manv','tennv'),'recursive'=>-1));
            $this->Z1kenhnv->recursive = 0;
		      $this->set('z1kenhnvs',  $this->Z1kenhnv->find('all',array('conditions'=>array('loaikenh_id'=>$loaikenh_id,'nhanvien_id'=>$nhanvien_id,'ngaykt_hd'=>null))));
            $loaikenhs = $this->Z1kenhnv->Loaikenh->find('list',array('fields'=>array('loaikenh')));
        }
        $this->set(compact('nhanvien_id','loaikenh_id','loaikenhs','donvi_id','n','tendv','tenloai','donvicha'));
	}
    
    function kenhdbexcel($donvi_id=null,$loaikenh_id=null,$nhanvien_id=0)
	{
		$this->layout='';
		//$nhanvien_id =$this->data['Phankenh']['nhanvien'];
        $n=$this->Nhanvien->find('first',array('conditions'=>array('id'=>$nhanvien_id),
                'fields'=>array('donvi_id','manv','tennv'),'recursive'=>-1));
        $tendv=$this->Zdv->find('first',array(
                'conditions'=>array('donvi_id'=>$donvi_id),
                'fields'=>array('tendv','tendvcha'),'recursive'=>-1));
        $tenloai=$this->Loaikenh->find('first',array(
                'conditions'=>array('id'=>$loaikenh_id),
                'fields'=>array('loaikenh'),'recursive'=>-1));
        $ngaybd = date('Y-m-d',strtotime($this->data['Kenh']['ngaybd']));
        $ngaykt = date('Y-m-d',strtotime($this->data['Kenh']['ngaykt']));
		if ($nhanvien_id==0)
		{
                 //cong tac diem ban
                $this->z1xemkenh->recursive = 0;
				$this->set('z1xemkenhs', $this->Z1xemkenh->find('all',
                            array('conditions'=>array('and'=>array('loaikenh_id'=>$loaikenh_id,'Z1xemkenh.donvi_id'=>$donvi_id,
                                                                 'ngaytao between ? and ?'=>array($ngaybd,$ngaykt))),
                                )));
                //thong tin thi truong
                $this->z2xemkenh->recursive = 0;
				$this->set('z2xemkenhs', $this->Z2xemkenh->find('all',
                            array('conditions'=>array('and'=>array('loaikenh_id'=>$loaikenh_id,'Z2xemkenh.donvi_id'=>$donvi_id,
                                                                 'ngaytao between ? and ?'=>array($ngaybd,$ngaykt))),
                                )));
                //tan suat cham soc
                $this->z3xemkenh->recursive = 0;
				$this->set('z3xemkenhs', $this->Z3xemkenh->find('all',
                            array('conditions'=>array('and'=>array('loaikenh_id'=>$loaikenh_id,'Z3xemkenh.donvi_id'=>$donvi_id,
                                                                 'ngaydi between ? and ?'=>array($ngaybd,$ngaykt))),
                                )));
				$loaikenh = $this->Loaikenh->find('list',array('fields'=>array('loaikenh')));
		} else {
                
                 //cong tac diem ban
                $this->z1xemkenh->recursive = 0;
				$this->set('z1xemkenhs', $this->Z1xemkenh->find('all',
                            array('conditions'=>array('and'=>array('loaikenh_id'=>$loaikenh_id,'nhanvien_id'=>$nhanvien_id,
                                                                 'ngaytao between ? and ?'=>array($ngaybd,$ngaykt))),
                                )));
                //thong tin thi truong
                $this->z2xemkenh->recursive = 0;
				$this->set('z2xemkenhs', $this->Z2xemkenh->find('all',
                            array('conditions'=>array('and'=>array('loaikenh_id'=>$loaikenh_id,'nhanvien_id'=>$nhanvien_id,
                                                                 'ngaytao between ? and ?'=>array($ngaybd,$ngaykt))),
                                )));
                //tan suat cham soc
                $this->z3xemkenh->recursive = 0;
				$this->set('z3xemkenhs', $this->Z3xemkenh->find('all',
                            array('conditions'=>array('and'=>array('loaikenh_id'=>$loaikenh_id,'nhanvien_id'=>$nhanvien_id,
                                                                 'ngaydi between ? and ?'=>array($ngaybd,$ngaykt))),
                                )));
				$loaikenh = $this->Loaikenh->find('list',array('fields'=>array('loaikenh')));
		}
		$this->set(compact('nhanvien_id','loaikenh','n','tendv','tenloai','ngaybd','ngaykt'));
	}
    
	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid z1kenhnv', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('z1kenhnv', $this->Z1kenhnv->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Z1kenhnv->create();
			if ($this->Z1kenhnv->save($this->data)) {
				$this->Session->setFlash(__('The z1kenhnv has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The z1kenhnv could not be saved. Please, try again.', true));
			}
		}
		$loaikenhs = $this->Z1kenhnv->Loaikenh->find('list');
		$donvis = $this->Z1kenhnv->Donvi->find('list');
		$nhanviens = $this->Z1kenhnv->Nhanvien->find('list');
		$this->set(compact('loaikenhs', 'donvis', 'nhanviens'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid z1kenhnv', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Z1kenhnv->save($this->data)) {
				$this->Session->setFlash(__('The z1kenhnv has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The z1kenhnv could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Z1kenhnv->read(null, $id);
		}
		$loaikenhs = $this->Z1kenhnv->Loaikenh->find('list');
		$donvis = $this->Z1kenhnv->Donvi->find('list');
		$nhanviens = $this->Z1kenhnv->Nhanvien->find('list');
		$this->set(compact('loaikenhs', 'donvis', 'nhanviens'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for z1kenhnv', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Z1kenhnv->delete($id)) {
			$this->Session->setFlash(__('Z1kenhnv deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Z1kenhnv was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
